<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2023 https://www.meoyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 喵云科技 【https://www.meoyun.com】
// +----------------------------------------------------------------------


namespace app\api\controller;


use app\common\controller\BaseMeowAdmin;
use app\common\service\JsonService;
use think\Exception\HttpResponseException;
use app\common\cache\UserTokenCache;

class BaseApi extends BaseMeowAdmin
{

    public function getUserId()
    {
        $token = $this->request->header('token');

        if (empty($token)) {
            JsonService::throw('请求参数缺少token', 420);
        }

        $userInfo = (new UserTokenCache())->getUserInfo($token);

        if (empty($userInfo)) {
            JsonService::throw('登录超时，请重新登录', 420);
        }

        if ($userInfo["user_id"]) {
            return $userInfo["user_id"];
        }
        JsonService::throw('用户信息获取失败', 420);
    }

    public function getUserIdNoAuth()
    {
        $token = $this->request->header('token');

        $userInfo = (new UserTokenCache())->getUserInfo($token);

        if (!empty($userInfo) && $userInfo["user_id"]) {
            return $userInfo["user_id"];
        } else {
            return 0;
        }
    }
}